﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using Data.Model;
using DataAccess.AccountManage;
using DataAccess.BikeManage.logical;
using Ninject;
using WinForm.Helper;

namespace WinForm.UserManage
{
    public partial class ModifyCardInfo : Form
    {
        private Zq58Logical zq58Logical = new Zq58Logical();
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IAccount _account = Kernel.Get<IAccount>();
        private CpuAccount _ca = null;
        private jsh_account _ja = null;

        public ModifyCardInfo()
        {
            InitializeComponent();
            InitalData();
        }

        private void InitalData()
        {
            try
            {
                cmbGender.SelectedIndex = 0;

                cmbNation.DataSource = new List<string>() { "汉", "蒙古", "回", "藏", "维吾尔", "苗", "彝", "壮", "布依", "朝鲜", "满", "侗", "瑶", "白", "土家", "哈尼", "哈萨克", "傣", "黎", "僳僳", "佤", "畲", "高山", "拉祜", "水", "东乡", "纳西", "景颇", "柯尔克孜", "土", "达斡尔", "仫佬", "羌", "布朗", "撒拉", "毛南", "仡佬", "锡伯", "阿昌", "普米", "塔吉克", "怒", "乌孜别克", "俄罗斯", "鄂温克", "德昂", "保安", "裕固", "京", "塔塔尔", "独龙", "鄂伦春", "赫哲", "门巴", "珞巴", "基诺族" };
                cmbNation.SelectedIndex = 0;
                //检查cpu卡
                //设备没有初始化也可以进入系统，只是和设备相关的功能不能使用
                //初始化设备
                
                ResultInfo<string> equip = zq58Logical.CheckEquipment();
                if (equip.ResultNumber != ResultEnum.Success)
                {
                    MyMessageBox.ShowMessageBoxInfo("请注意，读卡设备没有连上！");
                }
                else
                {
                    ResultInfo<CpuAccount> ca = zq58Logical.ReadAccountInfo();
                    if (ca.ResultNumber != ResultEnum.Success)
                    {
                        MyMessageBox.ShowMessageBoxInfo("请将卡放在读卡器上！");
                    }
                    else
                    {
                        _ca = ca.Obj;
                        ResultInfo<jsh_account> ja= _account.GetAccount(ca.Obj.CpuCardNo);
                        if(ja.ResultNumber==ResultEnum.Success)
                        {
                            _ja = ja.Obj;
                            txtName.Text = ja.Obj.username;
                            cmbGender.SelectedIndex = cmbGender.Items.IndexOf(ja.Obj.sex);
                            cmbNation.SelectedIndex = cmbNation.Items.IndexOf(ja.Obj.nation);
                            dateBirth.Value = DateTime.Now;
                            lbIdType.Text = ca.Obj.IdCardType;
                            lbIdNo.Text = ca.Obj.IdCardNo;
                            lbIdGov.Text = ja.Obj.fzdw;
                            txtMobile.Text = ja.Obj.mobile;
                            txtPhone.Text = ca.Obj.Mobile;
                            lbHomeAddr.Text = ja.Obj.addr1;
                            txtNowAddr.Text = ja.Obj.addr2;
                            cmbCpuVersion.SelectedIndex = cmbCpuVersion.Items.IndexOf(ca.Obj.CpuCardVersion);
                            lbCpuNo.Text = ca.Obj.CpuCardNo;
                            lbInitialMoney.Text = ja.Obj.balance.ToString();
                            lbCzMoney.Text = "0";
                            lbTotalMoney.Text = lbInitialMoney.Text;
                            lbReceipt.Text = ja.Obj.deposit.ToString();
                            dateValide.Value = Converts.ConvertToNormalDate(ja.Obj.edate);
                            cmbCpuType.SelectedIndex = cmbCpuType.Items.IndexOf(ca.Obj.CpuCardType);
                            if (ca.Obj.CpuCardStatus == CpuCardStatus.Open)
                            {
                                radioButton1.Checked = true;
                            }
                            else if (ca.Obj.CpuCardStatus == CpuCardStatus.Logout)
                            {
                                radioButton2.Checked = true;
                            }
                            else if (ca.Obj.CpuCardStatus == CpuCardStatus.Loss)
                            {
                                radioButton3.Checked = true;
                            }
                            else
                            {
                                radioButton1.Checked = true;
                            }
                            txtMark.Text = ja.Obj.memo;
                            try
                            {
                                MemoryStream myStream = new MemoryStream();
                                foreach (byte a in ja.Obj.photo)
                                {
                                    myStream.WriteByte(a);
                                }
                                Image myImage = Image.FromStream(myStream);
                                myStream.Close();
                                this.pictureBox1.Image = myImage;
                                this.pictureBox1.Refresh();
                            }
                            catch (Exception e)
                            {
                                
                            }
                            txtFee.Text = ca.Obj.Fee;
                        }else
                        {
                            MyMessageBox.ShowMessageBoxInfo("数据库中没有查找到相关用户记录，请联系开发商！");
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                if(_ca==null||_ja==null)
                {
                    MyMessageBox.ShowMessageBoxInfo("当前没有获取到用户卡数据！");
                    return;
                }
                //获取cpu卡用户信息
                CpuAccount cpuAccount = _ca;
                cpuAccount.CpuCardNo = lbCpuNo.Text;
                cpuAccount.CpuCardVersion = cmbCpuVersion.Text;
                
                if (radioButton1.Checked)
                {
                    cpuAccount.CpuCardStatus = CpuCardStatus.Open;
                }
                else if (radioButton2.Checked)
                {
                    cpuAccount.CpuCardStatus = CpuCardStatus.Logout;
                }
                else if (radioButton3.Checked)
                {
                    cpuAccount.CpuCardStatus = CpuCardStatus.Loss;
                }
                else
                {
                    cpuAccount.CpuCardStatus = CpuCardStatus.Open;
                }
                cpuAccount.CpuCardType = cmbCpuType.Text;
                cpuAccount.DisplayName = txtName.Text;
                cpuAccount.Fee = txtFee.Text;
                cpuAccount.Address = lbHomeAddr.Text;
                cpuAccount.IdCardNo = lbIdNo.Text;
                cpuAccount.IdCardType = lbIdType.Text;
                cpuAccount.Mobile = txtMobile.Text;
                cpuAccount.Phone = txtPhone.Text;
                cpuAccount.Sex = cmbGender.Text;
                cpuAccount.Nation = cmbNation.Text;

                this.Cursor = System.Windows.Forms.Cursors.WaitCursor; //出现忙碌光标

                ResultInfo<byte[]> writeAccount = zq58Logical.WriteAccountInfo(cpuAccount);
                if (writeAccount.ResultNumber == ResultEnum.Success)
                {
                    //写入数据库
                    jsh_account jshAccount = _ja;

                    jshAccount.addr1 = lbHomeAddr.Text;
                    jshAccount.addr2 = txtNowAddr.Text;
                    jshAccount.balance = Converts.ConvertToDecimal(lbTotalMoney.Text);
                    jshAccount.cardtype = cmbCpuType.Text;
                    jshAccount.closedate = dateValide.Value;
                    jshAccount.edate = dateValide.Value.ToString();
                    jshAccount.fzdw = lbIdGov.Text;
                    jshAccount.validate = dateValide.Value.ToString();
                    jshAccount.memo = txtMark.Text;
                    jshAccount.mobile = txtMobile.Text;
                    jshAccount.nation = cmbNation.Text;
                    
                    jshAccount.sdate = DateTime.Now.ToString();
                    jshAccount.sex = cmbGender.Text;
                    jshAccount.status = cpuAccount.CpuCardStatus;
                    jshAccount.telephone = cpuAccount.Phone;
                    jshAccount.username = cpuAccount.DisplayName;
                    jshAccount.zzhm = cpuAccount.IdCardNo;
                    jshAccount.zzlx = cpuAccount.IdCardType;

                    long id= _account.CreateUpdateAccount(jshAccount);
                    if (id>0)
                    {
                        
                            MessageBox.Show(string.Format("保存成功！初始金额：{0}元，余额：{1}元!        ", lbInitialMoney.Text, lbTotalMoney.Text));
                            this.Dispose();
                    }
                    else
                    {
                        zq58Logical.WriteAccountInfo(_ca);//恢复cpu卡信息
                        errorMessage += "保存到数据库失败！";
                    }
                }
                else
                {
                    errorMessage += writeAccount.Describe;
                }
            }
            catch (Exception e1)
            {
                errorMessage += e1.Message;
            }
            if(!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxInfo(errorMessage);
            }
        }
    }
}
